<?php

Class OtherAreaModel extends Model {

    /**
     * 根据父ID 查询 地址信息
     * @return array status 状态  msg 回馈信息 data 返回数据
     */
    public function address() {
        $id = $_REQUEST['id'];
        if ($id == '') {
            $arr['status'] = TRUE;
            $arr['msg']    = '';
            return $arr;
        }

        $map['parent_id'] = $id;

        $list = M('Area')->field('area_id,name')
                ->where($map)
                ->select();
        if (empty($list)) {
            $arr['status'] = FALSE;
            $arr['msg']    = '操作失败';
            $arr['data']   = $list;
        } else {
            $arr['status'] = TRUE;
            $arr['msg']    = '操作成功';
            $arr['data']   = $list;
        }

        return $arr;
    }

    /**
     * 添加收货地址
     */
    public function insert_address() {
        R('Core/Index/initUser');
        $_POST['user_id'] = FrameAppAction::$Cache['user_id'];

        if (is_numeric($_POST['phoneSection']) && is_numeric($_POST['phoneCode']) && !empty($_POST['phoneSection']) && !empty($_POST['phoneCode'])) {
            $_POST['tel'] = $_POST['phoneSection'] . '-' . $_POST['phoneCode'];
        }
        if ($_POST['tel'] != '' && is_numeric($_POST['phoneExt']) && !empty($_POST['phoneExt'])) {
            $_POST['tel'] .= '-' . $_POST['phoneExt'];
        }

        if ($_POST['isdefault'] == 1) {
            $where['user_id'] = FrameAppAction::$Cache['user_id'];
            $dot['isdefault'] = 0;
            $tru              = M('User_address')->where($where)
                    ->data($dot)
                    ->save();
        }

        $Result = M('user_address')->data($_POST)->add();

        if ($Result) {
            $arr['status'] = TRUE;
            $arr['status'] = '添加成功';
            $arr['data']   = $Result;
        } else {
            $arr['status'] = FALSE;
            $arr['msg']    = '添加失败,请重试';
        }
        return $arr;
    }

    /**
     * 获取该用户前10条收货地址
     * @return array
     */
    public function my_all_address() {
        R('Core/Index/initUser');
        $map['user_id'] = FrameAppAction::$Cache['user_id'];

        $Result = M('User_address')->where($map)
                ->limit(10)
                ->order('id desc')
                ->select();

        return $Result;
    }

    /**
     * 获取当前用户 ID 值的收货地址信息
     * @return array
     */
    public function my_one_address() {
        R('Core/Index/initUser');
        $id = intval($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;

        $map['id']      = $id;
        $map['user_id'] = FrameAppAction::$Cache['user_id'];
        $Result         = M('User_address')->where($map)->find();

        if (!empty($Result['tel'])) {
            $arr                    = explode('-', $Result['tel']);
            $Result['phoneSection'] = $arr[0];
            $Result['phoneCode']    = $arr[1];
            $Result['phoneExt']     = $arr[2];
        }

        return $Result;
    }

    /**
     * 获取所有省、市、区 信息
     * @return array
     */
    public function all_address() {
        $list = M('Other_area')->getField('area_id,name');

        return $list;
    }

    /**
     * 修改当前用户 ID 值的收货地址信息
     * @return array 返回数组 $arr
     *          $arr['status']  操作状态 true=成功 false=失败 
     *          $arr['msg']     回馈信息 
     *          $arr['data']    失败返回操作数据ID
     */
    public function update_address() {
        R('Core/Index/initUser');
        $id = intval($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;

        $data['consignee'] = trim($_REQUEST['consignee'], ' ');
        $data['province']  = $_REQUEST['province'];
        $data['city']      = $_REQUEST['city'];
        $data['area']      = $_REQUEST['area'];
        $data['address']   = $_REQUEST['address'];
        $data['zipcode']   = $_REQUEST['zipcode'];
        if (is_numeric($_REQUEST['phoneSection']) && is_numeric($_REQUEST['phoneCode']) && !empty($_REQUEST['phoneSection']) && !empty($_REQUEST['phoneCode'])) {
            $data['tel'] = $_REQUEST['phoneSection'] . '-' . $_REQUEST['phoneCode'];
        }
        if ($data['tel'] != '' && is_numeric($_REQUEST['phoneExt']) && !empty($_REQUEST['phoneExt'])) {
            $data['tel'] .= '-' . $_REQUEST['phoneExt'];
        }

        $data['isdefault'] = intval($_REQUEST['isdefault']) ? 1 : 0;
        $data['mobile']    = $_REQUEST['mobile'];

        if ($data['isdefault'] == 1) {
            $where['user_id'] = FrameAppAction::$Cache['user_id'];
            $dot['isdefault'] = 0;
            $tru              = M('User_address')->where($where)
                    ->data($dot)
                    ->save();
        }

        $map['id']      = $id;
        $map['user_id'] = FrameAppAction::$Cache['user_id'];
        $tru            = M('User_address')->where($map)
                ->data($data)
                ->save();

        if ($tru) {
            $arr['status'] = TRUE;
            $arr['msg']    = '修改成功!';
        } else {
            $arr['status'] = FALSE;
            $arr['msg']    = '操作失败，请重试!';
            $arr['data']   = $id;
        }

        return $arr;
    }

    /**
     * 删除当前用户 ID 值的收货地址信息
     * @return array 返回数组 $arr
     *          $arr['status']  操作状态 true=成功 false=失败 
     *          $arr['msg']     回馈信息 
     */
    public function del_address() {
        R('Core/Index/initUser');
        $id = intval($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;

        $map['id']      = $id;
        $map['user_id'] = FrameAppAction::$Cache['user_id'];
        $tru            = M('User_address')->where($map)->delete();

        if ($tru) {
            $arr['status'] = TRUE;
            $arr['msg']    = '删除成功!';
        } else {
            $arr['status'] = FALSE;
            $arr['msg']    = '删除失败，请重试!';
        }

        return $arr;
    }

    /**
     * 修改当前ID值的收货信息为改用户默认收货地址
     * @return array
     */
    public function edit_isdefault() {
        R('Core/Index/initUser');
        $id = intval($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;

        if (empty($id)) {
            $arr['status'] = FALSE;
            $arr['msg']    = '删除失败，请重试!';
            return $arr;
        }

        $where['user_id'] = FrameAppAction::$Cache['user_id'];
        $dot['isdefault'] = 0;
        $tru              = M('User_address')->where($where)
                ->data($dot)
                ->save();

        $data['isdefault'] = 1;
        $map['id']         = $id;
        $map['user_id']    = FrameAppAction::$Cache['user_id'];
        $trus              = M('User_address')->where($map)
                ->data($data)
                ->save();

        if ($trus) {
            $arr['status'] = TRUE;
            $arr['msg']    = '设置成功!';
        } else {
            $arr['status'] = FALSE;
            $arr['msg']    = '操作失败!';
        }

        return $arr;
    }

    public function get_name_by_id($id) {
        return $this->where(array('area_id' => $id))->getField('name');
    }

}

?>
